//
//  ListViewView.m
//  SimpleHappy
//
//  Created by 张超 on 15/11/25.
//  Copyright © 2015年 RockyFung. All rights reserved.
//  心愿单

#import "ListViewView.h"
#import "ItemView.h"
#import "Define.h"
#import "SHListModel.h"
#import "ListCell.h"
#import "SHCoreData.h"
#import "UIImageView+WebCache.h"
#import "DetailController.h"
#import "SHCoreData.h"

@interface ListViewView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSString *cellTitle;

@end

@implementation ListViewView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [super myTitleLabel].text = @"心愿单";
    [[super registBtn]removeFromSuperview];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, KScreenHeight / 10.42, KScreenWidth, KScreenHeight) style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[ListCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = KScreenHeight / 9; // 行高
    
     self.array = [NSMutableArray arrayWithArray:[SHCoreData findAll]];
    
    [self.view addSubview:_tableView];
    
    
}

#pragma mark - 返回按钮的方法
- (void)backBtnAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - 返回分区数、分区下行数、返回cell、返回表头高度
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHListModel *model = [[SHListModel alloc]init];
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    model = self.array[indexPath.row];
    cell.listTitleLabel.text = model.title;
    // 图片解析完成后刷新section显示图片
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.cellTitle = model.title;
    return cell;
}

#pragma mark - 点击cell传model进详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHListModel *model = self.array[indexPath.row];
    DetailController *detailVc = [[DetailController alloc]init];
    detailVc.isDiscover = YES;
    detailVc.targetUrl = model.openUrl;
    
    [self.tableView removeFromSuperview];
    [self.navigationBar removeFromSuperview];
    [self pushViewController:detailVc animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:YES animated:YES];
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 从数据源中删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"%@ %@", tableView, indexPath);
        [SHCoreData deleteModel:self.cellTitle];
        [self.array removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        
    
    }

}


@end
